﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class members_members : System.Web.UI.Page
{
    public class sortByTime : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            inquiry temp1 = (inquiry)x;
            inquiry temp2 = (inquiry)y;
            if (temp1.SubmitTime > temp2.SubmitTime)
            {
                return 1;
            }
            return 0;
        }
    }
    db database;
    ArrayList inquiries;
    ArrayList panels;
    protected void Page_Load(object sender, EventArgs e)
    {

        database = new db();
        inquiries = database.getInquiries();
        IComparer myComp = new sortByTime();
        inquiries.Sort(myComp);
        string name = database.getUserName(Convert.ToInt32(User.Identity.Name));
        this.lblMemberName.Text = "Welcome: " + name;
        int unreadMessages = database.getUnreadMessage();
        this.btnView.Text="View Messages("+unreadMessages.ToString()+")";
        panels = new ArrayList();
        panels.Add(this.pnlAdd);
        //panels.Add(this.pnlOptions);//prolly don't wanna remove this
        panels.Add(this.pnlRemove);
        panels.Add(pnlViewFeedback);
        panels.Add(pnlAddCountry);


        if (Request["page"] == "messages")
        {
            //display message
            if(string.IsNullOrEmpty(Request["id"]))
            {
                this.Response.Redirect(this.Request.Url.AbsolutePath + "?page=messages&id=0");
            }
        }
        if(Request["pass"] == "0")
        {
            this.lbUpdateInformation.Text = Request["message"];
        }
        else if(Request["pass"]=="1")
        {
            this.lbUpdateInformation.Text = Request["message"];
        }
        


    }
    private void disablePanels()
    {
        foreach (Panel p in panels)
        {
            p.Enabled = false;
            p.Visible = false;
        }
    }
    protected void Remove_Click(object sender, EventArgs e)
    {
        disablePanels();
        //get list of all ocation (name, id and country)
        //fille ddl like name - country, use dictionary to hold id against string
        ArrayList removeableDestination = database.getDestinations();
        foreach(destinations dest in removeableDestination)
        {
            this.ddlLocations.Items.Add(dest.Name + "-" + dest.Country);
        }
        this.pnlRemove.Enabled = true;
        this.pnlRemove.Visible = true;
    }
    private void viewForms(int id)
    {
        disablePanels();
        this.ddlFeedback.Items.Clear();
        for (int i = 0; i < inquiries.Count; i++)
        {
            inquiry myInquiry = (inquiry)inquiries[i];
            //find out if query has a response
            string response;
            if (myInquiry.Responded)
            {
                response = "R";
            }
            else
            {
                response = "NR";
            }
            this.ddlFeedback.Items.Add(i.ToString()+ " "+response + " - " + myInquiry.Surname + "," + myInquiry.Name);
        }
        this.ddlFeedback.SelectedIndex = id;
        this.updateFeedbackFields(id);
        this.pnlViewFeedback.Enabled = true;
        this.pnlViewFeedback.Visible = true;

    }
   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        disablePanels();
        //get countries, if none error out
        //else add to list and do below
        ArrayList countries = database.getCountries();
        if (countries.Count <= 0)
        {
            this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=0&message=Can't add new destination, database contains NO countries.");
            //FAIL CODE NOM COUTRIES STOP THEM
        }
        foreach (string country in countries)
        {
            this.ddlCountries.Items.Add(country);
        }
     
        this.pnlAdd.Enabled = true;
        this.pnlAdd.Visible = true;

    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        viewForms(0);
    }
    private void updateFeedbackFields(int index)
    {
        inquiry inq = (inquiry)inquiries[index];
        DateTime recievedTime = new DateTime();
        recievedTime = recievedTime.AddTicks(inq.SubmitTime);
        this.lblRecievedTime.Text = recievedTime.ToShortDateString() + ", " + recievedTime.ToShortTimeString();
        this.lblUsersName.Text = inq.Name + " " + inq.Surname;
       
        this.lblComments.Text =inq.Comments;
        if (inq.Responded)
        {
            this.lblIsRead.Text = "Yes";
            this.btnMarkRead.Text = "Mark as Unread";
        }
        else
        {
            this.btnMarkRead.Text = "Mark As Read";
            this.lblIsRead.Text = "No";
        }
        this.lblDestinationName.Text = database.getDestinationName(inq.Destination);
        this.lblUsersEmail.Text = inq.Email;
        return;
        
    }
    protected void btnDeleteLocation_Click(object sender, EventArgs e)
    {
        disablePanels();
        ArrayList removeableDestination = database.getDestinations();
        string selected = this.ddlLocations.SelectedValue;
        foreach (destinations dest in removeableDestination)
        {
            if (selected == (dest.Name + "-" + dest.Country))
            {
                int id = dest.Id;
                if (database.removeDestination(id) !=1)
                {
                    this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=" + 0 + "&message=deleted "+dest.Name+"-"+dest.Country+" Succesfully");
                }
                this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=" + 1 + "&message=filed to delete:"+dest.Name+"-"+dest.Country);
            }
        }
    }
    protected void btnAddDestination_Click(object sender, EventArgs e)
    {
        disablePanels();
        if (this.txtName.Text.Trim() != "" && this.txtDescription.Text.Trim() != "" && this.txtPrice.Text.Trim()!="")
        {
            if (this.fuImage.HasFile)
            {
                this.fuImage.SaveAs(@"C:\inetpub\wwwroot\images\" + fuImage.FileName);
                int result = database.addDestination(this.txtName.Text.Trim(), this.ddlCountries.SelectedValue, this.txtDescription.Text.Trim(), @"images/" + fuImage.FileName, Convert.ToInt32(this.txtPrice.Text.Trim()));
                if (result == 1)
                {
                    this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=1&message=Added " + this.txtName.Text.Trim());
                }
                this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=0&message=Failed to add  " + this.txtName.Text.Trim());
            }
            this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=0&message=No Image Selected, did not add destination " + this.txtName.Text.Trim());
        }
        this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=0&message=Not all forms filled out, failed to add " + this.txtName.Text.Trim());
    }
    protected void ddlFeedback_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.updateFeedbackFields(this.ddlFeedback.SelectedIndex);
    }
    protected void btnMarkRead_Click(object sender, EventArgs e)
    {
        int selectedID = this.ddlFeedback.SelectedIndex;
        inquiry lookingAt = (inquiry)inquiries[selectedID];   
        if(lookingAt.Responded)
        {
            lookingAt.Responded = true;
            database.markAsRead(lookingAt.Id, 1);
        }
        else
        {
            lookingAt.Responded = false;
            database.markAsRead(lookingAt.Id, 0);
        }
        this.ddlFeedback.Items.Clear();
        this.Response.Redirect(this.Request.Url.AbsolutePath + "?page=messages&id=" + selectedID);

    }
    protected void btnAddACountry_Click(object sender, EventArgs e)
    {
        
        if (!String.IsNullOrEmpty(this.txtCountry.Text.Trim()))
        {

            if (this.fuCountryPic.HasFile)
            {
                this.fuCountryPic.SaveAs(@"C:\inetpub\wwwroot\images\" + fuCountryPic.FileName);
                database.addCountry(this.txtCountry.Text.Trim(), @"images/" + fuCountryPic.FileName);
                this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=1&message=Added the country: " + this.txtCountry.Text.Trim());
            }

        }
        this.Response.Redirect(this.Request.Url.AbsolutePath + "?pass=0&message=Failed to add the country into the datbase.");
    }
    protected void btnAddCountry_Click(object sender, EventArgs e)
    {
        disablePanels();
        this.pnlAddCountry.Enabled = true;
        this.pnlAddCountry.Visible = true;
    }
    protected void btnLogOut_Click1(object sender, EventArgs e)
    {
        this.Response.Redirect(@"/Default.aspx");
    }
}